package com.xinsoft.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinsoft.common.exception.BDException;
import com.xinsoft.entity.dto.*;
import com.xinsoft.entity.po.MesFeedingPlan;
import com.xinsoft.entity.po.MesFeedingPlanDetail;
import com.xinsoft.entity.vo.MesFeedingPlanVo;
import com.xinsoft.mapper.MesFeedingPlanDetailMapper;
import com.xinsoft.service.MesFeedingPlanDetailService;
import com.xinsoft.service.MesFeedingPlanService;
import com.xinsoft.mapper.MesFeedingPlanMapper;
import com.xinsoft.utils.TokenUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
* @author hsm
* @description 针对表【mes_feeding_plan(投料计划)】的数据库操作Service实现
* @createDate 2023-07-24 15:10:35
*/
@Service
public class MesFeedingPlanServiceImpl extends ServiceImpl<MesFeedingPlanMapper, MesFeedingPlan>
    implements MesFeedingPlanService{


    @Resource
    public MesFeedingPlanDetailService mesFeedingPlanDetailService;

    @Resource
    public MesFeedingPlanDetailMapper mesFeedingPlanDetailMapper;

    /**
     * 分页接口
     * @param param
     * @return
     */
    @Override
    public IPage<MesFeedingPlanVo> findPageListByParam(MesFeedingPlanPageParam param) {
        //分页
        Page<MesFeedingPlanVo> page = new Page<MesFeedingPlanVo>(param.getCurrentPage(), param.getPageSize());
        IPage<MesFeedingPlanVo> resultPage = this.baseMapper.findListByParam(page, param);
        //返回值
        return resultPage;
    }

    /**
     * 新增接口
     * @param mesFeedingPlanDTO
     * @return
     */
    @Override
    public MesFeedingPlan addMesFeedingPlan(MesFeedingPlanDTO mesFeedingPlanDTO) {

        //判断编号否重复
        QueryWrapper<MesFeedingPlan> codeQuery = new QueryWrapper<>();
        codeQuery.select("id");
        codeQuery.eq("feeding_no",mesFeedingPlanDTO.getFeedingNo());
        List codes = this.list(codeQuery);
        if(CollectionUtil.isNotEmpty(codes)) {
            throw new BDException("操作失败，投料计划编号重复！");
        }

        MesFeedingPlan mesFeedingPlan = new MesFeedingPlan();
        BeanUtils.copyProperties(mesFeedingPlanDTO,mesFeedingPlan);

        mesFeedingPlan.setAuditStatus(0);
        mesFeedingPlan.setAuditId(null);
        save(mesFeedingPlan);

        // 保存明细
        if(CollectionUtil.isNotEmpty(mesFeedingPlanDTO.getMesFeedingPlanDetailList())){
            MesFeedingPlanDetail mesFeedingPlanDetail;
            for (MesFeedingPlanDetailDTO mesFeedingPlanDetailDTO : mesFeedingPlanDTO.getMesFeedingPlanDetailList()) {
                mesFeedingPlanDetail = new MesFeedingPlanDetail();
                BeanUtils.copyProperties(mesFeedingPlanDetailDTO,mesFeedingPlanDetail);
                mesFeedingPlanDetail.setFeedingPlanId(mesFeedingPlan.getId());
                mesFeedingPlanDetailService.save(mesFeedingPlanDetail);
            }
        }

        return mesFeedingPlan;
    }

    /**
     * 变更接口
     * @param mesFeedingPlanDTO
     * @return
     */
    @Override
    public MesFeedingPlan editMesFeedingPlan(MesFeedingPlanDTO mesFeedingPlanDTO) {
        //判断编号否重复
        QueryWrapper<MesFeedingPlan> codeQuery = new QueryWrapper<>();
        codeQuery.select("id");
        codeQuery.eq("feeding_no",mesFeedingPlanDTO.getFeedingNo());
        codeQuery.ne("id",mesFeedingPlanDTO.getId());
        List codes = this.list(codeQuery);
        if(CollectionUtil.isNotEmpty(codes)) {
            throw new BDException("操作失败，投料计划编号重复！");
        }

        MesFeedingPlan mesFeedingPlan = new MesFeedingPlan();
        BeanUtils.copyProperties(mesFeedingPlanDTO,mesFeedingPlan);

        mesFeedingPlan.setAuditStatus(0);
        mesFeedingPlan.setAuditId(null);
        save(mesFeedingPlan);

        //删除明细
        QueryWrapper detailQuery = new QueryWrapper<>();
        detailQuery.eq("feeding_plan_id",mesFeedingPlanDTO.getId());
        mesFeedingPlanDetailService.remove(detailQuery);
        // 保存明细
        if(CollectionUtil.isNotEmpty(mesFeedingPlanDTO.getMesFeedingPlanDetailList())){
            for (MesFeedingPlanDetailDTO mesFeedingPlanDetailDTO : mesFeedingPlanDTO.getMesFeedingPlanDetailList()) {
                MesFeedingPlanDetail detail = new MesFeedingPlanDetail();
                BeanUtils.copyProperties(mesFeedingPlanDTO,detail);
                mesFeedingPlanDetailService.save(detail);
            }
        }
        return mesFeedingPlan;
    }

    /**
     * 获取详情
     * @param id
     * @return
     */
    @Override
    public MesFeedingPlanDTO getMesFeedingPlan(Integer id) {
        MesFeedingPlanDTO result = this.baseMapper.selectInfo(id);
        if (result == null) {
            return null;
        }
        List<MesFeedingPlanDetailDTO> detailList =  mesFeedingPlanDetailMapper.findListByParam(id);
        if(CollectionUtil.isNotEmpty(detailList)){
            result.setMesFeedingPlanDetailList(detailList);
            List<MesFeedingPlanDetailTree> trees = new ArrayList<MesFeedingPlanDetailTree>();

            for (MesFeedingPlanDetailDTO mesFeedingPlanDetailDTO : detailList) {
                MesFeedingPlanDetailTree tree = new MesFeedingPlanDetailTree();

                BeanUtils.copyProperties(mesFeedingPlanDetailDTO,tree);
                tree.setBomDetailId(mesFeedingPlanDetailDTO.getBomDetailId().toString());
                tree.setBomParentId(mesFeedingPlanDetailDTO.getBomParentId().toString());
                tree.setText(mesFeedingPlanDetailDTO.getMaterialName()+" "+mesFeedingPlanDetailDTO.getMaterialNo()+" "+mesFeedingPlanDetailDTO.getFeedingNum());
                trees.add(tree);
            }

            result.setMesFeedingPlanDetailTree(build(trees));
        }
        return result;
    }

    /**
     * 审核
     * @param ids
     * @return
     */
    @Override
    public void audit(List<Integer> ids) {
        UpdateWrapper<MesFeedingPlan> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("audit_status",1);//审核
        updateWrapper.set("audit_id", TokenUtil.getUserId());//设置审核人
        updateWrapper.in("id",ids);
        updateWrapper.eq("do_delete",0);
        this.update(updateWrapper);
    }

    /**
     * 反审核
     * @param ids
     * @return
     */
    @Override
    public void deAudit(List<Integer> ids) {
        UpdateWrapper<MesFeedingPlan> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("audit_status",0);//审核
        updateWrapper.set("audit_id", TokenUtil.getUserId());//设置审核人
        updateWrapper.in("id",ids);
        updateWrapper.eq("do_delete",0);
        this.update(updateWrapper);
    }

    /**
     * 构建树
     * @param nodes
     * @return
     * @param nodes
     */
    private MesFeedingPlanDetailTree build(List<MesFeedingPlanDetailTree> nodes) {

        if (nodes == null) {
            return null;
        }
        List<MesFeedingPlanDetailTree> topNodes = new ArrayList<MesFeedingPlanDetailTree>();

        for (MesFeedingPlanDetailTree children : nodes) {

            String pid = children.getBomParentId();
            if (pid == null || "0".equals(pid)) {
                topNodes.add(children);

                continue;
            }

            for (MesFeedingPlanDetailTree parent : nodes) {
                String bomDetailId = parent.getBomDetailId();
                if (bomDetailId != null && bomDetailId.equals(pid)) {
                    parent.getChildren().add(children);
                    continue;
                }
            }
        }
        return topNodes.get(0);
    }

    /**
     * 根据id 获取明细行
     * @param ids
     * @return
     */
    @Override
    public List<MesFeedingPlanDetailDTO> getItemList(List<Integer> ids) {
        List<MesFeedingPlanDetailDTO> result = new ArrayList<>();
        List<MesFeedingPlanDetailDTO> detailList = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(ids)){
            for (Integer id : ids) {
                detailList.addAll(mesFeedingPlanDetailMapper.findListByParam(id));
            }

            List idList = new ArrayList<>();
            for (MesFeedingPlanDetailDTO mesFeedingPlanDetailDTO : detailList) {
                idList.add(mesFeedingPlanDetailDTO.getBomParentId());
            }
            for (MesFeedingPlanDetailDTO mesFeedingPlanDetailDTO : detailList) {
                if( !idList.contains(mesFeedingPlanDetailDTO.getId())){
                    result.add(mesFeedingPlanDetailDTO);
                }
            }
        }
        return result;
    }

}




